from datetime import timezone,timedelta
import datetime,traceback
import datetime
class ToolKit:
    TIMEZONE_VALUE = 8
    TIMEZONE = timezone(timedelta(hours=+8))
    @staticmethod
    def translateDruation(seconds):
      hours = int(seconds / 3600)
      seconds -= (hours * 3600)
      mins = int(seconds / 60)
      seconds = int(seconds - (mins * 60))
      return "{}:{}:{}".format(hours,mins,seconds)

    @staticmethod
    def getTimeNow(human=False):
        now = datetime.datetime.now().astimezone(ToolKit.TIMEZONE)
        if(human):
            now = now.strftime("%Y-%m-%d %H:%M:%S")
        return now

    @staticmethod
    def isTomorrow(date):
        now = ToolKit.getTimeNow()
        return date.day > now.day

    @staticmethod
    def getNextTime(hour):
        date = ToolKit.getTimeNow()
        Log.d("Current Time:{}".format(date))
        delta = abs(hour - date.hour)
        if(date.hour > hour):
            date = date + datetime.timedelta(days=1)
            date = date - datetime.timedelta(hours=delta)
        else:
            date = date + datetime.timedelta(hours=delta)
        date = date - datetime.timedelta(minutes=date.minute, seconds=date.second, microseconds=date.microsecond)
        Log.d("Next Time is: {}".format(date))
        return date

    @staticmethod
    def calcIntervalSeconds(value):
        now = ToolKit.getTimeNow()
        Log.d("calc interval seconds from:{} to {}".format(now, value))
        return (value - ToolKit.getTimeNow()).total_seconds()
class Log:
    DEBUG = 0
    INFO = 1
    WARN = 2
    ERROR = 3
    LEVEL = INFO

    @staticmethod
    def setLevel(level):
        LEVEL = level;

    @staticmethod
    def __loadBaseFmt(msg, level):
        info = traceback.extract_stack()[-4]
        flag = {
            0:"D",
            1:"I",
            2:"W",
            3:"E"
        }
        return "{} | {} | {}:{} | {}".format(flag.get(level, None), ToolKit.getTimeNow(True), info[0], info[1], msg)
    
    @staticmethod
    def __addColorFmt(msg, type):
        color = {
            0:"0;37;40",
            1:"0;32;47",
            2:"4;35;47",
            3:"5;31;47"
        }
        return "\033[{}m{}\033[0m".format(color.get(type, None),msg)

    @staticmethod
    def __printMsg(msg, level):
        if(level >= Log.LEVEL):
            info = Log.__loadBaseFmt(msg, level)
            info = Log.__addColorFmt(info, level)
            print(info, flush=True)


    @staticmethod
    def d(msg):
        Log.__printMsg(msg, Log.DEBUG)

    @staticmethod
    def i(msg):
        Log.__printMsg(msg, Log.INFO)

    @staticmethod
    def w(msg):
        Log.__printMsg(msg, Log.WARN)

    @staticmethod
    def e(msg):
        Log.__printMsg(msg, Log.ERROR)
